package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.BevelBorder;

public class ChatterWindows extends JFrame {
	public static String TEMP_VIEW_STRING = "TPks43789234kjd2132KL13";
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	 JButton buttonCall, buttonNewMess, buttonHide,buttonClear,buttonFonts;
	 JToggleButton buttonFile;
	 PanelPic panelPic;
	 JTextArea textAreaView;
	 JTextField textFieldSend;
	 JTextArea textAreaTempView;
	 JCheckBox checkTempView;
	 Image imageWaiting, imageReading;
	 JPanel panelFiles;
	 Thread threadBiger;
	 FontWindow fontWindow;
	 ThemesWindow themesWindow;
	 String name,myName;

	 JLabel lblReadAlready;

	@SuppressWarnings("unused")
	private String key;

	public ChatterWindows(String title,String key) throws HeadlessException {
		super(title);


		this.fontWindow = new FontWindow(this);
		this.themesWindow = new ThemesWindow(this);


		this.key = key;

		this.buttonCall = new JButton();
		this.buttonCall.setBackground(new Color(122, 196, 121));
		this.buttonCall.setIcon(new ImageIcon(Chatter_Util.getImage("/test/small_arrow.png")));
		this.buttonCall.setHorizontalTextPosition(SwingConstants.RIGHT);
		this.buttonCall.setHorizontalAlignment(SwingConstants.LEFT);
		this.buttonCall.setMargin(new Insets(0, 2, 0, 2));


		this.buttonNewMess = new JButton();
		this.buttonNewMess.setBackground(new Color(254, 227, 180));
		this.buttonNewMess.setIcon(new ImageIcon(Chatter_Util.getImage("/test/small_mess.png")));
		this.buttonNewMess.setHorizontalTextPosition(SwingConstants.RIGHT);
		this.buttonNewMess.setHorizontalAlignment(SwingConstants.LEFT);
		this.buttonNewMess.setMargin(new Insets(0, 2, 0, 2));

		this.buttonHide = new JButton();
		this.buttonHide.setBackground(new Color(127, 169, 204));
		this.buttonHide.setIcon(new ImageIcon(Chatter_Util.getImage("/test/small_hide.png")));
		this.buttonHide.setHorizontalTextPosition(SwingConstants.RIGHT);
		this.buttonHide.setHorizontalAlignment(SwingConstants.LEFT);
		this.buttonHide.setMargin(new Insets(0, 2, 0, 2));

		this.buttonFile = new JToggleButton();
		this.buttonFile.setBackground(new Color(222, 95, 95));
		this.buttonFile.setHorizontalAlignment(SwingConstants.LEFT);
		this.buttonFile.setIcon(new ImageIcon(Chatter_Util.getImage("/test/small_folder.png")));
		this.buttonFile.setMargin(new Insets(0, 2, 0, 2));

		this.buttonClear = new JButton();
		this.buttonClear.setBackground(new Color(222, 95, 95));
		this.buttonClear.setHorizontalTextPosition(SwingConstants.RIGHT);
		this.buttonClear.setHorizontalAlignment(SwingConstants.LEFT);
		this.buttonClear.setIcon(new ImageIcon(Chatter_Util.getImage("/test/delete.png")));
		this.buttonClear.setMargin(new Insets(0, 2, 0, 2));

		this.buttonFonts = new JButton("Fonts...");
		this.buttonFonts.setBackground(new Color(56, 131, 209));
		this.buttonFonts.setHorizontalTextPosition(SwingConstants.RIGHT);
		this.buttonFonts.setHorizontalAlignment(SwingConstants.LEFT);
		this.buttonFonts.setIcon(new ImageIcon(Chatter_Util.getImage("/test/small_font.png")));
		this.buttonFonts.setMargin(new Insets(0, 2, 0, 2));

		this.checkTempView = new JCheckBox("TV");
		this.checkTempView.setToolTipText("Typing view: Cho phép người dùng xem nội dung khi bạn gõ.");


		this.imageWaiting = Chatter_Util.getImage("/test/waiting.png");
		this.imageReading = Chatter_Util.getImage("/test/reading.png");

		this.panelPic = new PanelPic(imageReading);
		this.textAreaView = new JTextArea();
		this.textFieldSend = new JTextField();
		this.textAreaTempView = new JTextArea();

		this.lblReadAlready = new JLabel("Chưa đọc");

		this.panelFiles = new JPanel();
		panelFiles.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.LOWERED));

		JPanel panel = new JPanel();
		panel.setLayout(null);

		panel.add(buttonCall);
		panel.add(buttonHide);
		panel.add(buttonNewMess);
		panel.add(buttonFile);
		panel.add(buttonClear);
		panel.add(buttonFonts);
		panel.add(checkTempView);
		panel.add(lblReadAlready);

		panel.add(panelPic);

		panel.add(textFieldSend);
		JScrollPane jScrollPane = new JScrollPane(textAreaView);
		panel.add(jScrollPane);

		JScrollPane jScrollPaneTempView = new JScrollPane(textAreaTempView);
		panel.add(jScrollPaneTempView);

		textAreaView.setAutoscrolls(true);

		buttonHide.setBounds(5, 60, 50, 40);
		buttonCall.setBounds(5, 105, 50, 40);
		buttonNewMess.setBounds(5, 150, 50, 40);
		buttonFile.setBounds(5, 245, 50, 40);
		//buttonClear.setBounds(5, 290, 100, 40);
		buttonClear.setBounds(5, 195, 50, 40);
		checkTempView.setBounds(5, 240, 50, 30);
		buttonFonts.setBounds(5, 285, 50, 40);

		textFieldSend.setBounds(60, 200, 200, 30);
		textFieldSend.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.LOWERED));



		jScrollPane.setBounds(60, 5, 200, 190);
		jScrollPaneTempView.setBounds(60, 240, 200, 80);
		// jScrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

		this.lblReadAlready.setBounds(60, 305, 120, 50);

		textAreaView.setMinimumSize(new Dimension(300, 240));
		textAreaView.setLineWrap(true);
		textAreaView.setWrapStyleWord(true);

		textAreaTempView.setLineWrap(true);
		textAreaTempView.setWrapStyleWord(true);
		textAreaTempView.setBackground(new Color(237, 237, 237));

		checkTempView.setSelected(true);

		panelPic.setBounds(5, 5, 50, 50);
		panelPic.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		panelPic.setBackground(new Color(165, 216, 176));


		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);

		JMenu menuMoreAction = new JMenu("More...");
		menuBar.add(menuMoreAction);
		JMenuItem menuItemFont = new JMenuItem("Fonts...");
		JMenuItem menuItemFile = new JMenuItem("File...");
		JMenuItem menuItemThemes = new JMenuItem("Themes");
		menuMoreAction.add(menuItemFont);
		menuMoreAction.add(menuItemFile);
		menuMoreAction.add(menuItemThemes);


		menuItemFile.setEnabled(false);
		menuItemFont.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				fontWindow.setLocationRelativeTo(ChatterWindows.this);
				fontWindow.setFontTemp(textAreaView.getFont());
				fontWindow.setVisible(true);

			}
		});

		menuItemThemes.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				themesWindow.setLocationRelativeTo(ChatterWindows.this);
				themesWindow.setVisible(true);

			}
		});

		this.getContentPane().add(panel);

		// test
		buttonFile.addActionListener(new ActionListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent e) {
				if (threadBiger != null && threadBiger.isAlive()) {
					threadBiger.stop();
				}

				if (buttonFile.isSelected()) {
					threadBiger = new ChatterWindowThreadOpen(
							ChatterWindows.this, true);
					threadBiger.start();
				} else {
					threadBiger = new ChatterWindowThreadOpen(
							ChatterWindows.this, false);
					threadBiger.start();
				}

				// try {
				// Process p = new ProcessBuilder("explorer.exe",
				// "/select,C:\\a.csv").start();
				//
				//
				// } catch (IOException e1) {
				// // TODO Auto-generated catch block
				// e1.printStackTrace();
				// }
			}
		});

		buttonClear.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				textAreaView.setText("");

			}
		});

		this.addWindowFocusListener(new WindowFocusListener() {

			@Override
			public void windowLostFocus(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowGainedFocus(WindowEvent e) {
				fontWindow.setVisible(false);
				setIconImage(Toolkit.getDefaultToolkit().getImage(RunServer.class.getResource("/test/chat.png")));
			}
		});

		buttonFonts.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				fontWindow.setLocationRelativeTo(ChatterWindows.this);
				fontWindow.setFontTemp(textAreaView.getFont());
				fontWindow.setVisible(true);

			}
		});

		textAreaView.setFont(new Font("Times New Roman", 0, 14));
		setIconImage(Toolkit.getDefaultToolkit().getImage(RunServer.class.getResource("/test/chat.png")));
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setSize(285, 405);
		setLocationRelativeTo(null);
		buttonFile.setEnabled(false);
//		setVisible(true);

		buttonFile.setVisible(false);
		buttonFonts.setVisible(false);

	}
	public void reading() {
		panelPic.setImage(imageReading);
		panelPic.setBackground(new Color(165, 216, 176));
		panelPic.setToolTipText("Your friend is reading your message");
		orderReading(true);

	}

	public void watting() {
		panelPic.setImage(imageWaiting);
		panelPic.setBackground(new Color(236, 163, 163));
		panelPic.setToolTipText("Your friend may be not reading your message");
		orderReading(false);
	}

	public void scrollToEnd(){
		textAreaView.setCaretPosition(textAreaView.getDocument().getLength());
	}
	public void addViewData(String data){
		textAreaView.setText(textAreaView.getText()+"\n"+data);
	}
	public String getTypingData(){
		return textFieldSend.getText();
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new ChatterWindows("test","");
	}
	public void newMess(boolean b) {
		if(b){
			setIconImage(Toolkit.getDefaultToolkit().getImage(RunServer.class.getResource("/test/chat_new_mess.png")));
		}else{
			setIconImage(Toolkit.getDefaultToolkit().getImage(RunServer.class.getResource("/test/chat.png")));
		}

	}
	public void orderReading(boolean b) {
		if(b){
			setIconImage(Toolkit.getDefaultToolkit().getImage(RunServer.class.getResource("/test/chat_reading.png")));
		}else{
			setIconImage(Toolkit.getDefaultToolkit().getImage(RunServer.class.getResource("/test/chat.png")));
		}

	}

	public void refeshThemes() {
		SwingUtilities.updateComponentTreeUI(this);
		SwingUtilities.updateComponentTreeUI(this.fontWindow);
		SwingUtilities.updateComponentTreeUI(this.themesWindow);
	}

}
